package cn.net.withub.dataCollector.common.filter;

import cn.net.withub.dataCollector.common.utils.CommonUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class InterceptFilter implements Filter{

	public static List<String> createUser = new ArrayList<String>();

	private Logger log= LoggerFactory.getLogger(this.getClass());
	public List<String> nofilterList =new ArrayList<String>();


	public void init(FilterConfig filterConfig) throws ServletException {
		nofilterList.add("149.0.13");
		nofilterList.add("154.0.66");
		nofilterList.add("151.11.182");
		nofilterList.add("151.11.181");
		nofilterList.add("151.0.26");
		nofilterList.add("127.0.0.1");
		nofilterList.add("149.0.150");
		nofilterList.add("149.0.156");
	}

	public boolean noFilterIp(String ip){
		for(String ips:nofilterList){
			if(ip.startsWith(ips)){
				return true;
			}
		}
		return false;
	}
	public void destroy() {

	}




	public void doFilter(ServletRequest arg0, ServletResponse arg1, FilterChain arg2) throws IOException, ServletException {
		HttpServletRequest request = (HttpServletRequest)arg0;
		HttpServletResponse response = (HttpServletResponse)arg1;
		HttpSession session = request.getSession();
		//初始化配置参数
		String url = request.getRequestURI();

		String url1 = request.getServletPath();

		String urlPara = request.getQueryString();

        System.out.println("scsdcsd");
        System.out.println("scsdcsd");
        System.out.println("scsdcsd");
        System.out.println("scsdcsd");
        System.out.println("scsdcsd");
        System.out.println("sc裁定书sdcsd");
        System.out.println("scsdcsd");
        System.out.println("scsdcsd");

		String ip = CommonUtils.getRemoteIpAddr(request);

		if((url.indexOf("/resources/")!=-1&&noFilterIp(ip)) || url.indexOf("/logController/")!=-1){
			arg2.doFilter(arg0, arg1);
		}else{
			arg2.doFilter(arg0, arg1);
		}
	}

	public String objToString(Object obj){
		return obj==null?null:obj.toString().trim();
	}
}
